package com.getpebble.android.framework.util;

import android.graphics.Bitmap;
import com.google.common.primitives.UnsignedInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class PblBitmap {
    public final byte[] mData;
    public final UnsignedInteger mFlags;
    public final short mHeight;
    public final UnsignedInteger mRowLengthBytes;
    public final short mWidth;
    public final short mX;
    public final short mY;

    private PblBitmap(UnsignedInteger rowLengthBytes, UnsignedInteger flags, short x, short y, short width, short height, byte[] data) {
        this.mRowLengthBytes = rowLengthBytes;
        this.mFlags = flags;
        this.mX = x;
        this.mY = y;
        this.mWidth = width;
        this.mHeight = height;
        this.mData = data;
    }

    public static PblBitmap fromAndroidBitmap(Bitmap bmp) {
        int height = bmp.getHeight();
        int width = bmp.getWidth();
        int rowLengthBytes = ((width + 31) / 32) * 4;
        ByteBuffer buffer = ByteBuffer.allocate(rowLengthBytes * height);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        for (int row = 0; row < height; row++) {
            for (int w = 0; w < rowLengthBytes / 4; w++) {
                int xOffset = w * 32;
                int word = 0;
                int x = 0;
                while (x < 32 && xOffset + x < width) {
                    int i;
                    if ((bmp.getPixel(xOffset + x, row) & 16777215) > 1) {
                        i = 1;
                    } else {
                        i = 0;
                    }
                    word |= i << x;
                    x++;
                }
                buffer.putInt(word);
            }
        }
        return new PblBitmap(UnsignedInteger.fromIntBits(rowLengthBytes), UnsignedInteger.fromIntBits(4096), (short) 0, (short) 0, (short) width, (short) height, buffer.array());
    }

    public ByteBuffer toByteBuffer() {
        ByteBuffer buffer = ByteBuffer.allocate(this.mData.length + 12);
        buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.putShort(this.mRowLengthBytes.shortValue());
        buffer.putShort(this.mFlags.shortValue());
        buffer.putShort(this.mX);
        buffer.putShort(this.mY);
        buffer.putShort(this.mWidth);
        buffer.putShort(this.mHeight);
        buffer.put(this.mData);
        buffer.rewind();
        return buffer;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PblBitmap that = (PblBitmap) o;
        if (this.mHeight != that.mHeight) {
            return false;
        }
        if (this.mWidth != that.mWidth) {
            return false;
        }
        if (this.mX != that.mX) {
            return false;
        }
        if (this.mY != that.mY) {
            return false;
        }
        if (!Arrays.equals(this.mData, that.mData)) {
            return false;
        }
        if (this.mFlags == null ? that.mFlags != null : !this.mFlags.equals(that.mFlags)) {
            return false;
        }
        if (this.mRowLengthBytes != null) {
            if (this.mRowLengthBytes.equals(that.mRowLengthBytes)) {
                return true;
            }
        } else if (that.mRowLengthBytes == null) {
            return true;
        }
        return false;
    }

    public int hashCode() {
        int result;
        int hashCode;
        int i = 0;
        if (this.mRowLengthBytes != null) {
            result = this.mRowLengthBytes.hashCode();
        } else {
            result = 0;
        }
        int i2 = result * 31;
        if (this.mFlags != null) {
            hashCode = this.mFlags.hashCode();
        } else {
            hashCode = 0;
        }
        hashCode = (((((((((i2 + hashCode) * 31) + this.mX) * 31) + this.mY) * 31) + this.mWidth) * 31) + this.mHeight) * 31;
        if (this.mData != null) {
            i = Arrays.hashCode(this.mData);
        }
        return hashCode + i;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("PebbleBitmap");
        sb.append("{height=").append(this.mHeight);
        sb.append(", width=").append(this.mWidth);
        sb.append(", y=").append(this.mY);
        sb.append(", x=").append(this.mX);
        sb.append(", flags=").append(this.mFlags);
        sb.append(", rowLengthBytes=").append(this.mRowLengthBytes);
        sb.append('}');
        return sb.toString();
    }
}
